package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.auth.bean.MerchantVerifyOutput;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.MerchantInfoAuthContract;
import com.tianyang.yukuaifuzs.business.main.presenter.MerchantInfoAuthPresenter;
import com.tianyang.yukuaifuzs.common.utils.CommonUtils;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;

/**
 * Description：商户信息支付宝、微信授权
 * Created by xyj on 2024/11/15
 */
public class MerchantInfoAuthActivity extends BaseTitleActivity implements MerchantInfoAuthContract.View {

    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.codeTv)
    TextView codeTv;
    @BindView(R.id.signStatusTv)
    TextView signStatusTv;
    @BindView(R.id.wxCodeSdv)
    SimpleDraweeView wxCodeSdv;
    @BindView(R.id.refreshTv)
    TextView refreshTv;

    private MerchantInfoAuthContract.Presenter mPresenter;

    private PosUserOutput mOutput;
    private boolean isWx = false;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_info_auth;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("商户信息");
        mPresenter = new MerchantInfoAuthPresenter(this);
        isWx = getIntent().getBooleanExtra(MainConfig.INTENT_IS_WX, false);
        mOutput = (PosUserOutput) getIntent().getSerializableExtra(MainConfig.INTENT_POS_USER_OUTPUT);
        if (mOutput != null) {
            if (isWx) {
                nameTv.setText("微信子商户号");
                codeTv.setText(VerifyUtil.checkString(mOutput.getWechatCode()));
                if (TextUtils.equals("AUTHORIZED", mOutput.getWechatAuthorizeStatus())) {
                    signStatusTv.setText("已授权");
                    signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                } else {
                    signStatusTv.setText("未授权");
                    signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
                }
            } else {
                nameTv.setText("支付宝子商户号");
                codeTv.setText(VerifyUtil.checkString(mOutput.getAliCode()));
                if (TextUtils.equals("AUTHORIZED", mOutput.getAlipayAuthorizeStatus())) {
                    signStatusTv.setText("已授权");
                    signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                } else {
                    signStatusTv.setText("未授权");
                    signStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
                }
            }
            mPresenter.getMerchantVerify(mOutput.getPhone());
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        refreshTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isWx) {
                    CommonUtils.copyText(mContext, mOutput.getWechatCode());
                } else {
                    CommonUtils.copyText(mContext, mOutput.getAliCode());
                }
            }
        });
    }

    @Override
    public void showMerchantVerifyView(MerchantVerifyOutput output) {
        if (output != null) {
            if (isWx) {
                if (!TextUtils.isEmpty(output.getWeChatUrl())) {
                    wxCodeSdv.setImageURI(Uri.parse(output.getWeChatUrl()));
                }
            } else {
                if (!TextUtils.isEmpty(output.getAliUrl())) {
                    wxCodeSdv.setImageURI(Uri.parse(output.getAliUrl()));
                }
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantInfoAuthContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
